Skip to content

Conversation

@henrik0804
Copy link
Contributor

Preserve aliases in nested "through" joins when referencing the same table multiple times

While upgrading an older Laravel package and in turn eloquent-power-joins, a few of our queries broke because nested "through" relationships no longer respected table aliases when the same table was joined multiple times.

Example:

Comment::joinRelationship('user.commentsThroughPosts', [
    'user' => fn ($join) => $join->as('users_alias')->withTrashed(),
    'commentsThroughPosts' => [
        'posts' => fn ($join) => $join->as('posts_alias')->withTrashed(),
    ],
])->toSql();

Current output (using incorrect alias on the users table):
... inner join "posts" as "posts_alias" on "posts_alias"."user_id" = "users"."id"

Expected output (after checking the static cache for the alias on the far parent):
... inner join "posts" as "posts_alias" on "posts_alias"."user_id" = "users_alias"."id"

@luisdalmolin luisdalmolin merged commit 32ec75f into kirschbaum-development:master Oct 25, 2025
19 checks passed
@luisdalmolin
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants